<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">

</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addInfo"> 添加链接 </button>
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="deletegoods"> 删除勾选 </button>
                    <input type="file" id="excelFile" accept=".xls,.xlsx" style="display: none;" />
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>
            </script>

        </div>
    </div>
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <script src="/lib/axios/axios.js" charset="utf-8"></script>
    <script src="/lib/dayjs.js" charset="utf-8"></script>
    <script>

        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            let tableIns = table.render({
                elem: '#currentTableId',
                url: '/url/getDyData',
                toolbar: '#toolbarDemo',
                cols: [[
                    { type: "checkbox", width: 50 },
                    { field: 'url', minWidth: 100, title: '链接', sort: true },
                    { field: '设备编号', minWidth: 100, title: '设备编号', sort: true },
                    { field: '采集时间', minWidth: 100, title: '采集时间',sort:true,templet: function (d) {
                            // // console.log(d); // 得到当前行数据
                            // if (d.taskStatus == "掉线") {
                            //     return '<span style="color: #F00;font-weight:bold">掉线</span>'
                            // } else if (d.taskStatus == "空闲") {
                            //     return '<span style="color: #00CCFF;font-weight:bold">空闲</span>'
                            // } else if (d.taskStatus == "忙碌") {
                            //     return '<span style="color: #FFCC00;font-weight:bold">忙碌</span>'
                            // }
                            return dayjs(Number(d['采集时间'])).format('YYYY-MM-DD HH:mm:ss')
                        } },
                    { field: '当前福袋时间', minWidth: 100, title: '福袋剩余时间', sort: true },
                    { field: '采集时间', minWidth: 100, title: '预计开奖时间',sort:true,templet: function (d) {
                            const timeStr = d['当前福袋时间']
                            const [fen,miao] = timeStr.split(':')
                            let time = 0
                            time = (Number(fen) *60 + Number(miao)) * 1000
                            let 开奖时间 = dayjs(Number(d['采集时间'])).add(time, 'millisecond').format('YYYY-MM-DD HH:mm:ss')
                            return 开奖时间
                        } },
                    // { field: '当前福袋时间', minWidth: 100, title: '当前福袋时间', sort: true },
                    { field: '是否已使用', minWidth: 100, title: '是否已使用', sort: true,templet:function(d){
                        return `<span style="padding:2px;color:white;background:${d['是否已使用'] ? 'red' : 'green'}">${d['是否已使用']}</span>`
                    } },
                    {field: '福袋参与人数', minWidth: 100, title: '福袋参与人数', sort: true },
                    {field: '福袋发放数量', minWidth: 100, title: '福袋发放数量', sort: true },
                    {field: '链接下发次数', minWidth: 100, title: '链接下发次数', sort: true },
                    {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [50, 200, 1000, 2000, 5000],
                limit: 50,
                page: true
            });

            var laydate = layui.laydate;
            /**
             * toolbar监听事件
             */

            table.on('toolbar(currentTableFilter)', function (obj) {
                if(obj.event == 'addInfo'){
                    layer.open({
                        title: '添加链接',
                        type: 1,
                        area: ['500px', '320px'], // 自定义弹出框宽高
                        content: `
                            <form class="layui-form" style="padding: 20px;">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">视频链接</label>
                                    <div class="layui-input-block">
                                        <input type="string" name="url" required lay-verify="required" placeholder="请输入视频链接" autocomplete="off" class="layui-input"></input>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">采集时间</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="采集时间" required lay-verify="required" placeholder="请输入采集时间" autocomplete="off" class="layui-input"></input>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">当前福袋时间</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="当前福袋时间" required lay-verify="required" placeholder="请输入当前福袋时间" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <button type="button" class="layui-btn" id="saveBtn1">保存</button>
                                    </div>
                                </div>
                            </form>
        `,
                        success: function (layero, index) {
                            layui.use(['form'], function () {
                                var form = layui.form;

                                // 重新渲染表单
                                form.render();

                                // 绑定保存按钮的点击事件
                                $(layero).find('#saveBtn1').on('click', function () {
                                    let newUrl = $(layero).find('input[name="url"]').val().trim();
                                    let new采集时间 = $(layero).find('input[name="采集时间"]').val().trim();
                                    let new当前福袋时间 = $(layero).find('input[name="当前福袋时间"]').val().trim();
                                    if (newUrl.length <= 0) {
                                        return layer.msg('视频链接不能为空！');
                                    }
                                   /*  if (new采集时间.length <= 0) {
                                        return layer.msg('采集时间不能为空！');
                                    }
                                    if (new当前福袋时间.length <= 0) {
                                        return layer.msg('当前福袋时间不能为空！');
                                    } */

                                    // 发送 PUT 请求更新数据
                                    axios.post('/url/setNewDy', {
                                        url: newUrl,
                                        采集时间: new采集时间,
                                        当前福袋时间: new当前福袋时间
                                    })
                                    .then(function (res) {
                                        layer.closeAll(); // 关闭所有弹出框
                                        tableIns.reload(); // 刷新表格
                                        layer.msg('新增成功！');
                                    })
                                    .catch(function (err) {
                                        console.log("请求失败", err.response || err);
                                        layer.alert(err.message || "请求失败");
                                    });
                                });
                            });
                        }
                    });
                }else if (obj.event == 'resetInfo') {
                    let checkStatus = table.checkStatus('currentTableId')
                    let data = checkStatus.data;
                    if (data.length == 0) {
                        return
                    }

                    layer.confirm('真的重置这些数据么?', function (index) {
                        console.log(data)
                        let idArr = []
                        data.forEach(d => {
                            idArr.push(d._id)
                        })
                        $.ajax({
                            url: '/url/setDyStatus',
                            type: 'post',
                            data: {
                                idArr: JSON.stringify(idArr)
                            },
                            success: function () {
                                layer.msg("重置成功")
                                tableIns.reload({
                                    url: '/url/getDyData',
                                    method: 'get',
                                })
                            },
                            error: function (xhr, textstatus, thrown) {
                                layer.msg('接口有误');
                            }
                        })
                        layer.close(index);
                    });
                } else if (obj.event == 'deletegoods') {
                    let checkStatus = table.checkStatus('currentTableId')
                    let data = checkStatus.data;
                    if (data.length == 0) {
                        return
                    }

                    layer.confirm('真的删除这些数据么?', function (index) {
                        console.log(data)
                        let idArr = []
                        data.forEach(d => {
                            idArr.push(d._id)
                        })
                        $.ajax({
                            url: '/url/deleteData',
                            type: 'post',
                            data: {
                                idArr: JSON.stringify(idArr)
                            },
                            success: function () {
                                layer.msg("删除成功!")
                                tableIns.reload({
                                    url: '/url/getDyData',
                                    method: 'get',
                                })
                            },
                            error: function (xhr, textstatus, thrown) {
                                layer.msg('接口有误');
                            }
                        })
                        layer.close(index);
                    });
                }
            });


            table.on('tool(currentTableFilter)', function (obj) {
                const data = obj.data
                if (obj.event === 'deleteOne') {
                    layer.confirm('真的删除【' + data.url + '】么?', function (index) {
                        obj.del();
                        tableIns.reload({
                            url: '/url/delOne',
                            method: 'post',
                            where: {
                                _id: data._id
                            }
                        })
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    // 编辑按钮点击事件
                    layer.open({
                        title: '编辑链接',
                        type: 1,
                        area: ['500px', '400px'], // 自定义弹出框宽高
                        content: `
                            <form class="layui-form" style="padding: 20px;">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">视频链接</label>
                                    <div class="layui-input-block">
                                        <input type="string" name="url" required lay-verify="required" placeholder="请输入视频链接" autocomplete="off" class="layui-input" value="${data.url}"></input>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <button type="button" class="layui-btn" id="saveBtn">保存</button>
                                    </div>
                                </div>
                            </form>
                        `,
                        success: function (layero, index) {
                            layui.use(['form'], function () {
                                var form = layui.form;

                                // 重新渲染表单
                                form.render();

                                // 绑定保存按钮的点击事件
                                $(layero).find('#saveBtn').on('click', function () {
                                    let newUrl = $(layero).find('input[name="url"]').val().trim();
                                
                                    
                                    if (newUrl.length <= 0) {
                                        return layer.msg('视频链接不能为空！');
                                    }

                                    // 发送 PUT 请求更新数据
                                    axios.post('/url/updateDy', {
                                        _id: data._id,
                                        url: newUrl,
                                    
                                    })
                                    .then(function (res) {
                                        layer.closeAll(); // 关闭所有弹出框
                                        tableIns.reload(); // 刷新表格
                                        layer.msg('更新成功！');
                                    })
                                    .catch(function (err) {
                                        console.log("请求失败", err.response || err);
                                        layer.alert(err.message || "请求失败");
                                    });
                                });
                            });
                        }
                    });
                }
            });

        });
    </script>

</body>

</html>